package leadingIn;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import theBeans.student;


/**
 * Servlet implementation class readExcelServlet
 */
@WebServlet("/readExcelServlet")
public class readExcelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public readExcelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<student> initialList=new ArrayList<student>();
		// PoiUtil，能处理xlsx文件和xls文件
		String realFullFilePath=String.valueOf(request.getAttribute("realFullFilePath"));
		PoiUtil poi = new PoiUtil();
		poi.setFileName(realFullFilePath);
		for(int row=1;row<7;row++)
		{
			student studentOption=new student();
			for(int col=0;col<4;col++)
			{
				int cellValue=Integer.parseInt(poi.getCell(0, row, col));
				switch(col)
				{
				case(0):studentOption.setSid(cellValue);
					break;
				case(1):studentOption.setSchoolNum(cellValue);
					break;
				case(2):studentOption.setCredit(cellValue);
					break;
				case(3):studentOption.setIfConfirm(cellValue);
					break;
				default:
					break;
				}
				System.out.println("row:"+row+"\ncol:"+col);
				System.out.println(poi.getCell(0, row, col));
			}
			initialList.add(studentOption);
		}
		
		request.setAttribute("initialList", initialList);
		request.getRequestDispatcher("initializeServlet").forward(request, response);
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
